package com.yuyunbo.es.restclient.autoconfigure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: yuyunbo-1154363414@qq.com
 * @Date: 2020/4/20 14:12
 * @Description:
 */
@Configuration
@EnableConfigurationProperties(EsConfigProperties.class)
@ConditionalOnProperty(prefix = "es.restclient", value = "enable", matchIfMissing = true)
public class EsConfigBeanAutoconfigure {
    @Autowired
    private EsConfigProperties esConfigProperties;

    @Bean(name = "esApiService")
    @ConditionalOnMissingBean(EsApiServiceImpl.class)
    public EsApiServiceImpl initEsApiServiceImpl() {
        return new EsApiServiceImpl();
    }

    @Bean(name = "esRestClient")
    @ConditionalOnMissingBean(EsRestClient.class)
    public EsRestClient initEsRestClient() {
        return new EsRestClient(esConfigProperties.getEsNodes(), esConfigProperties.getEsPort(), esConfigProperties.getConnectTimeout(),
                esConfigProperties.getMaxRetryTimeoutMillis(), esConfigProperties.getMaxSize());
    }
}
